package net.asfun.jvalog.resource;

import java.util.Collection;

import javax.jdo.Query;

import net.asfun.jvalog.common.Jdoer;
import net.asfun.jvalog.common.JdoerFactory;
import net.asfun.jvalog.entity.Web;
import net.asfun.jvalog.vo.Pager;

public class WebDao {

	public static Collection<Web> listPublished() {
		return JdoerFactory.jdoWeb.more("flag > " + Web.FLAG_INVFRI);
	}

	public static Web getMySite() {
		Jdoer<Web> jdw = JdoerFactory.jdoWeb;
		Query query = jdw.prepareQuery();
		query.setFilter("flag < " + Web.FLAG_INVFRI);
		Web web = jdw.unique(query);
		if ( web == null ) {
			web = new Web();
			web.setDescription("A blog prowed by GAE JAVA");
			web.setSubtitle("Webcome to jvaLog");
			web.setTitle("jvaLog");
			web.setUrl("/");
			web.setFlag(Web.FLAG_MYSITE);
			jdw.save(web);
		}
		return web;
	}

	public static Collection<Web> listAll(Pager pager) {
		Jdoer<Web> jdw = JdoerFactory.jdoWeb;
		Query q = jdw.prepareQuery();
		q.setFilter("flag > " + Web.FLAG_MYSITE);
		pager.setTotal(jdw.more(q).size());
		q.setRange(pager.getStart(), pager.getEnd());
		return jdw.more(q);
	}
}
